package model.entity.container;

import model.exceptions.InvalidNameException;
import model.management.ProductGroupManager;

import common.Result;

/**
 * Describes a top-level <code>ProductContainer</code> that is a physical storage location.
 * 
 * @author Trevor Dixon, Ryan Cheatham
 */
public class StorageUnit extends ProductContainer
{
  private static final long serialVersionUID = -1857339114733975957L;

  /**
   * Constructs a new StorageUnit with the given name.
   * 
   * @param name
   *          The name of the new StorageUnit.
   * @throws InvalidNameException
   *           if the given name is invalid.
   */
  public StorageUnit(String name) throws InvalidNameException
  {
    super(name);
  }

  @Override
  public Result validateAddProductGroup(ProductGroup group)
  {
    Result result = ProductGroupManager.validateAddProductGroup(group);
    if (result.getStatus() == false)
      return result;

    for (ProductGroup subGroup : productGroups)
    {
      if (subGroup.getName().equals(group.getName()))
        return new Result(false, "Cannot add ProductGroup"
            + " - this StorageUnit already contains a ProductGroup with that name!");
    }

    return new Result(true);
  }

  @Override
  public boolean equals(Object obj)
  {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (!(obj instanceof StorageUnit))
      return false;
    StorageUnit other = (StorageUnit) obj;
    if (name == null)
    {
      if (other.name != null)
        return false;
    }
    else if (!name.equals(other.name))
      return false;
    return true;
  }

  @Override
  public String toString()
  {
    StringBuilder builder = new StringBuilder();
    builder.append("StorageUnit [name=");
    builder.append(name);
    builder.append("]");
    return builder.toString();
  }
}
